package com.core.common.utils;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.core.common.domain.response.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: maobao-parent
 * @description: 非标json 转换 bean
 * @author: honglei
 * @create: 2022-05-12 06:11
 */
@Slf4j
public class BeanConvertUtil {

    /***
     * 转小写
     * @param str
     * @return
     */
    public static String convertToCamelCase(String str){
        if (!StringUtils.hasLength(str)){
            return null;
        }
        str=str.replace(" ","");
        str=str.toLowerCase();

        StringBuffer sb = new StringBuffer();
        sb.append(str);
        int count = sb.indexOf("_");
        while(count!=0){
            int num = sb.indexOf("_",count);
            count = num + 1;
            if(num != -1){
                char ss = sb.charAt(count);
                if (ss > 96 && ss < 123 ){
                    char ia = (char) (ss - 32);
                    sb.replace(count , count + 1,ia + "");
                }
            }
        }
        String result = sb.toString().replaceAll("_","");
        return result;
    }

    public static Map convertMap(String jsonString){
        Map<String, Object> map= JSONUtil.toBean(jsonString, Map.class);
        log.info("BeanConvertUtil.convertMap:{}",map);
        return convertMapKeyToCamelCase(map);
    }

    private static Map convertMapKeyToCamelCase(Map<String, Object> map){
        log.info("BeanConvertUtil.convertMapKeyToCamelCase:{}",map);
        if (CollectionUtils.isEmpty(map) ){
            return map;
        }
        Map<String, Object> targetMap=new HashMap<>();
        for (String s : map.keySet()) {
            Object value=map.get(s);
            if (map.get(s) instanceof Map){
                Map subMap= (Map) value;
                value = convertMapKeyToCamelCase(subMap);
            }
            if (map.get(s) instanceof List){
                List subList= (List) value;
                if (! CollectionUtils.isEmpty(subList)){
                    List list=new ArrayList(subList.size());
                    for (Object o : subList) {
                        if (o instanceof Map){
                            value = convertMapKeyToCamelCase((Map<String, Object>) o);
                            list.add(value);
                        }
                    }
                    value=list;
                }
            }
            String strKey=convertToCamelCase(s);
            targetMap.put(strKey,value);
        }
        log.info("BeanConvertUtil.convertMapKeyToCamelCase result:{}",map);
        return targetMap;
    }


    public static <T> T toBean(String jsonString, Class<T> beanClass) {
        Map map= JSONUtil.toBean(jsonString, Map.class);
        Map sourceMap=convertMapKeyToCamelCase(map);
        return BeanUtil.mapToBean(sourceMap,beanClass,true);
    }

    public static void main(String[] args) {
        R r=toBean(sf,R.class);
        System.out.println(r);
    }


    private static String sf="{\n" +
            "    \"bizCode\": null,\n" +
            "    \"code\": 200,\n" +
            "    \"success\": true,\n" +
            "    \"message\": \"操作成功\",\n" +
            "    \"outMatch\": \"操作成功\"\n" +
            "}";

}
